package com.unitedvision.persistence.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.unitedvision.core.domain.CustomerType;

@Entity
@Table (name = "customer_type")
public class CustomerTypeEntity {
	private int id;
	private String name;
	private List<CustomerEntity> customerEntities;
	
	public CustomerTypeEntity() {
		super();
	}

	public CustomerTypeEntity(String name) {
		super();
		this.name = name;
	}
	
	public CustomerTypeEntity(CustomerType type) {
		super();
		name = type.getName();
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column (name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@OneToMany (targetEntity = CustomerEntity.class, mappedBy = "type", fetch = FetchType.LAZY)
	public List<CustomerEntity> getCustomerEntities() {
		return customerEntities;
	}

	public void setCustomerEntities(List<CustomerEntity> customerEntities) {
		this.customerEntities = customerEntities;
	}

	public CustomerType toCustomerType() {
		return new CustomerType(id, name);
	}
	
	public static CustomerTypeEntity create(String name) {
		return new CustomerTypeEntity(name);
	}
	
	public static CustomerTypeEntity create(CustomerType type) {
		return new CustomerTypeEntity(type);
	}

	@Override
	public String toString() {
		return "CustomerTypeEntity [id=" + id + ", name=" + name + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CustomerTypeEntity other = (CustomerTypeEntity) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
